﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using PublicClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PublicTuKu
{
    public partial class FormTuKu : Form
    {
        public FormTuKu()
        {
            InitializeComponent();
            // 报异常
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.listView1.Columns.Add("名称", 50);
            this.listView1.Columns.Add("路径", 50);

            UpDataList();
        }
        private void UpDataList()
        {
            this.listView1.BeginUpdate();
            this.listView1.Items.Clear();

            foreach (var item in TuKuFun.GetTuKuSingle().ListDwgFile)
            {
                AddOneToList(item, false);
            }

            this.listView1.EndUpdate();
        }
        public void InsertBlk()
        {
            var item = this.listView1.FocusedItem;
            if (null != item)
            {
                string str = item.SubItems[1].Text;

                ObjectId blockReord = PublicClass.CadUnits.InsertBlockFromDwgtoDatabase(str);

                while (true)
                {
                    BlockReference blk = new BlockReference(Point3d.Origin, blockReord);
                    SampleJigEnts entJig = new SampleJigEnts();
                    entJig.EntList.Add(blk);
                    var st = CadUnits.CurEdit().Drag(entJig);
                    if (st.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                    {
                        entJig.AddToDb();
                    }
                    else
                        break;
                }

            }
        }

        private void AddOneToList(string item, bool bUpdate = true)
        {
            this.listView1.BeginUpdate();

            string strName = System.IO.Path.GetFileNameWithoutExtension(item);
            ListViewItem itemView = new ListViewItem(strName);
            itemView.SubItems.Add(item);
            this.listView1.Items.Add(itemView);

            this.listView1.EndUpdate();
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
            dlg.Filter = "dwg文件|*.dwg";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (var item in dlg.FileNames)
                {
                    TuKuFun.GetTuKuSingle().ListDwgFile.Add(item);
                    AddOneToList(item);
                }
                TuKuFun.GetTuKuSingle().Save();
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            // 自己扩展,多选删除怎么操作?
            var item = this.listView1.FocusedItem;
            if (null != item)
            {
                TuKuFun.GetTuKuSingle().ListDwgFile.Remove(item.SubItems[1].Text);
                this.listView1.Items.Remove(item);
                TuKuFun.GetTuKuSingle().Save();
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CadUnits.CurDoc().SendStringToExecute("PublicTuKu_TuKuSub ", false, false, false);
        }
    }


    public class TuKuFun
    {
        public static PaletteSet g_paletteSet = null; //面板

        static private FormTuKu g_single = new FormTuKu();
        static private TuKuFun g_tukuFun = new TuKuFun();

        public List<string> ListDwgFile { get; set; }

        static public FormTuKu GetTuKuForm()
        {
            return g_single;
        }

        static public void ShowTuKu()
        {
            if (g_paletteSet == null || g_paletteSet.IsDisposed)
            {
                g_paletteSet = new PaletteSet("图库管理");
                g_paletteSet.Size = new Size(90, 600);
                GetTuKuForm().Anchor =
                     System.Windows.Forms.AnchorStyles.Top |
                     System.Windows.Forms.AnchorStyles.Left |
                     System.Windows.Forms.AnchorStyles.Right;
                GetTuKuForm().Dock = DockStyle.Fill;
                GetTuKuForm().TopLevel = false;
                GetTuKuForm().Location = new Point(0, 0);
                g_paletteSet.Add("屏幕菜单", GetTuKuForm());
            }

            g_paletteSet.Visible = true;
            g_paletteSet.Dock = DockSides.Left;
        }

        static public TuKuFun GetTuKuSingle()
        {
            if (g_tukuFun == null)
            {
                g_tukuFun = new TuKuFun();
            }
            return g_tukuFun;
        }

        private TuKuFun()
        {
            ListDwgFile = new List<string>();
            Load();
        }
        static private string Path()
        {
            string path = PublicClass.PublicNoCad.GetSelftUserPath();
            System.IO.Directory.CreateDirectory(path);
            return System.IO.Path.Combine(PublicClass.PublicNoCad.GetSelftUserPath(), "wangafeiTuKu.txt");
        }
        private void Load()
        {
            string[] lines = null;
            try
            {
                lines = System.IO.File.ReadAllLines(Path());
                ListDwgFile.AddRange(lines);
            }
            catch (System.Exception e)
            {
            }
        }

        public void Save()
        {
            try
            {
                using (var sr = new StreamWriter(Path()))
                {
                    foreach (var item in ListDwgFile)
                    {
                        sr.WriteLine(item);
                    }
                }
            }
            catch (System.Exception)
            {
            }
        }
    }
}
